package com.aube.interact.model;

import com.aube.rvi.util.StringUtils;

import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by huyaonan on 15/11/7.
 */
public class TimeLinePiece implements Serializable {

    public String type="";
    public String rid="";

    public String thumbnailCard="N";

    /**
     * 多镜头预载时间
     */
    public int preloadSeconds=0;

    public int startSeconds=0;
    public int endSeconds=0;
    public String title="";
    public String tlDesc="";//多镜头的红字

    public TimeLineItemDetail detail;

    public static TimeLinePiece parse(JSONObject jsonObject) {
        if(jsonObject == null)
            return new TimeLinePiece();
        TimeLinePiece data = new TimeLinePiece();
        data.type = jsonObject.optString("type");
        data.rid = jsonObject.optString("rid");
        data.thumbnailCard = jsonObject.optString("thumbnailCard");
        data.title = jsonObject.optString("title");
        data.tlDesc = jsonObject.optString("tlDesc");
        data.startSeconds = jsonObject.optInt("startSeconds");
        data.endSeconds = jsonObject.optInt("endSeconds");
        data.detail = TimeLineItemDetail.parse(jsonObject.optJSONObject("detail"));
        return data;
    }

    //视频的播放时间
    public long playTime=0;

    //卡片已经开始了多长时间
    public long timePassed = 0;

    public static final String TYPE_QA = "QA";
    public static final String TYPE_VOTE = "VOTE";
    public static final String TYPE_VS = "VS";
    public static final String TYPE_PIC = "PIC";
    public static final String TYPE_VIDEO= "MULTICAMERA";

    public boolean isIllegal = true;

    public int preLoadVideo = -1;

    public void addToPicMap(HashMap<Integer, String> maps) {
        if(detail != null)
            detail.addToPicMap(maps, startSeconds);
    }

    public int getTimeDown() {
        return detail == null ? 0 : detail.timedown;
    }

    public boolean isBlank() {
        return StringUtils.isBlank(type);
    }

    public int getPeriod() {
        int left = endSeconds - startSeconds;
        if(left < timePassed)
            return 0;
        return (int) (left - timePassed);
    }

    public String getNote() {
        String result = detail == null ? "" : detail.cardnote;
        if(StringUtils.isBlank(result))
            return "";
        return result+"，";
    }

    public String getImgUrl() {
        if(detail == null)
            return "";
        return detail.getImgUrl();
    }

    public String getImgTag() {
        if(detail == null)
            return "";
        return detail.getImgTag();
    }

    public List<OptionItem> getOptions() {
        if(detail == null || detail.optionList == null)
            return new ArrayList<>();
        return detail.optionList;
    }

    public int optionSize() {
        return getOptions().size();
    }

    public int getCountDownTime() {
        if(TYPE_QA.equalsIgnoreCase(type)) {
            if(detail.revealTheAnswer > startSeconds+timePassed)
                return (int) (detail.revealTheAnswer - startSeconds-timePassed);
        }
        return 0;
    }

    public boolean isVideoCard() {
        return TYPE_VIDEO.equalsIgnoreCase(type);
    }

    public String getOptionId(int index) {
        if(detail != null)
            return detail.getOptionId(index);
        return null;
    }

    public String getOptionValue(int index) {
        if(detail != null)
            return detail.getOptionValue(index);
        return "";
    }

    public int getOptionIndex(String optionId) {
        if(detail != null)
            return detail.getOptionIndex(optionId);
        return -1;
    }

    public boolean isCardFold() {
        return "Y".equalsIgnoreCase(thumbnailCard);
    }

}
